﻿using CcMath;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Demo
{
    public partial class RpChartDemo : Form
    {
        List<DataPoint> mSelDataPoints = new List<DataPoint>();
        public RpChartDemo()
        {
            InitializeComponent();
        }

        private void cmnToggleLegend_Click(object sender, EventArgs e)
        {
            plot.ShowLegend = !plot.ShowLegend;

            cmnToggleLegend.Text = plot.ShowLegend ? "隐藏图标" : "显示图标";
        }

        private void cmnClearMarkers_Click(object sender, EventArgs e)
        {
            mSelDataPoints.Clear();
            plot.ClearMarkers();
        }

        private void cmnPolar_Click(object sender, EventArgs e)
        {
            plot.PolarPlot = !plot.PolarPlot;
            cmnPolar.Text = plot.PolarPlot ? "直角坐标" : "极坐标";
        }

        private void cmnLockSerie_Click(object sender, EventArgs e)
        {
            if (plot.CurrHit.Series != null)
                plot.LockPlot(plot.CurrHit.Series.Name);
        }

        private void cmnEnterEnvelop_Click(object sender, EventArgs e)
        {
            plot.EnvelopMode = !plot.EnvelopMode;
            if (plot.EnvelopMode)
            {
                Cursor = Cursors.Cross;
                staInfo.Text = "点击取点，绘制包络线";
                cmnEnterEnvelop.Text = "结束包络线";
            }
            else
            {
                Cursor = Cursors.Default;
                staInfo.Text = "点击生成Marker";
                cmnEnterEnvelop.Text = "输入包络线";
            }
        }

        private void cmnClearEvelop_Click(object sender, EventArgs e)
        {
            plot.ClearEnvelops();
        }

        private void 默认线宽ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void plot_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (plot.CurrHit.Axis != null)
            {

                (double max, double min, double interval) axisValue = (plot.CurrHit.Axis.Maximum, plot.CurrHit.Axis.Minimum, plot.CurrHit.Axis.MajorGrid.Interval);
                FrmEditAxis frm = new FrmEditAxis(axisValue, "修改" + plot.CurrHit.Axis.AxisName + "轴");
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    plot.SetAxial(plot.CurrHit.Axis, frm.AxisValue.min, frm.AxisValue.max, frm.AxisValue.interval);

                }
            }
        }

        private void ctxtDefautWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar=='\r'&& int.TryParse(ctxtDefautWidth.Text, out int wid))
            {
                plot.DefaultWidth = wid;
            }
        }

        private void ctxtHighlightWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r' && int.TryParse(ctxtDefautWidth.Text, out int wid))
            {
                plot.HighlightWidth = wid;
            }
        }

        private void cmnSinglePlot_Click(object sender, EventArgs e)
        {
            plot.TwoPlotsMode = !plot.TwoPlotsMode;
            cmnSinglePlot.Text = plot.TwoPlotsMode ? "单图模式" : "双图模式";
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {

            Point32[] pointH = new Point32[361];
            Point32[] pointV = new Point32[361];

            for (int i = 0; i < 361; i++)
            {
                double y = 10*Math.Sin((i-180) * Math.PI / 180);
                pointH[i] = new Point32(i-180,y);
                y = 10 * Math.Cos((i - 180) * Math.PI / 180);
                pointV[i] = new Point32(i - 180, y);
            }
            
            if (plot.TwoPlotsMode)
            {
                plot.AddPattern("PatternH", pointH, 0);
                plot.AddPattern("PatternV", pointV, 1);
            }
            else
            {
                plot.AddPattern("PatternH", pointH, 0);
                plot.AddPattern("PatternV", pointV, 0);
            }
            plot.PlotReferLines();
        }
    }
}
